package com.bestvike.stone.spring.shiro.annotation.resolver;

import com.bestvike.stone.spring.shiro.aop.MethodInvocation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * 默认注解解析器
 */
public final class DefaultAnnotationResolver implements AnnotationResolver {
    /**
     * 获取方法上的指定类型注解
     */
    @Override
    public Annotation getAnnotation(MethodInvocation methodInvocation, Class<? extends Annotation> clazz) {
        if (methodInvocation == null) {
            throw new IllegalArgumentException("methodInvocation argument can not be null");
        }
        Method method = methodInvocation.getMethod();
        if (method == null) {
            String msg = MethodInvocation.class.getName() + " parameter incorrectly constructed. getMethod() returned null";
            throw new IllegalArgumentException(msg);
        }
        Annotation annotation = method.getAnnotation(clazz);
        if (annotation == null) {
            Object miThis = methodInvocation.getThis();
            annotation = miThis != null ? miThis.getClass().getAnnotation(clazz) : null;
        }
        return annotation;
    }
}
